from rest_framework.generics import ListAPIView,CreateAPIView
from users.models import User
from . import user_serializers
from meiduo_admin.my_paginate import MyPageNumberPagination



# 7.获取用户信息
class UserView(ListAPIView,CreateAPIView):
    # serializer_class = serializers.UserSerializer
    # queryset = User.objects.order_by.all()
    pagination_class = MyPageNumberPagination

    # 1.重写获取序列话器的方法
    def get_serializer_class(self):
        if self.request.method == "GET":
            return user_serializers.UserSerializer
        else:
            return user_serializers.UserAddSerializer

    # 2.重写获取数据源的方法
    def get_queryset(self):
        # 1,获取查询关键字
        keyword = self.request.query_params.get("keyword")
        # 2,判断是否有关键字
        if keyword:
            return User.objects.filter(username__contains=keyword).order_by("id").all()
        else:
            return User.objects.order_by("id").all()

    # 添加用户
    # def post(self,request):
        # # 1.获取数据
        # dict_data =request.data
        #
        # # 2.获取序列化器,校验,入库
        # serializer = self.get_serializer(dict_data)
        # serializer.is_valid(raise_exception=True)
        # serializer.save()
        #
        # #返回响应
        # return Response(serializer.data,status=201)
        # return self.create(request)